package com.gr.smartedubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.BusinessException;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.resource.DownloadCountUpdateDto;
import com.gr.smartedubackend.model.dto.resource.ResourceAddRequest;
import com.gr.smartedubackend.model.dto.resource.ResourceQueryRequest;
import com.gr.smartedubackend.model.dto.resource.ResourceUpdateRequest;
import com.gr.smartedubackend.model.entity.Resource;
import com.gr.smartedubackend.model.entity.User;
import com.gr.smartedubackend.service.ResourceService;
import com.gr.smartedubackend.service.UserService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Long> addResource(@RequestBody ResourceAddRequest resourceAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(resourceAddRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceAddRequest, resource);
        resource.setUploaderId(loginUser.getId());
        resource.setUploaderName(loginUser.getUserName());
        boolean result = resourceService.save(resource);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(resource.getId());
    }

    @GetMapping("/get")
    public BaseResponse<Resource> getResourceById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Resource resource = resourceService.getById(id);
        ThrowUtils.throwIf(resource == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(resource);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<Resource>> listResourceByPage(@RequestBody ResourceQueryRequest resourceQueryRequest) {
        ThrowUtils.throwIf(resourceQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = resourceQueryRequest.getCurrent();
        int pageSize = resourceQueryRequest.getPageSize();
        Page<Resource> resourcePage = resourceService.page(new Page<>(current, pageSize),
                resourceService.getQueryWrapper(resourceQueryRequest));
        return ResultUtils.success(resourcePage);
    }

    @PostMapping("/list")
    public BaseResponse<List<Resource>> listResource(@RequestBody ResourceQueryRequest resourceQueryRequest) {
        ThrowUtils.throwIf(resourceQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(resourceService.list(resourceService.getQueryWrapper(resourceQueryRequest)));
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateResource(@RequestBody ResourceUpdateRequest resourceUpdateRequest) {
        ThrowUtils.throwIf(resourceUpdateRequest == null || resourceUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceUpdateRequest, resource);
        boolean result = resourceService.updateById(resource);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/update/downloadCount")
    public BaseResponse<Boolean> updateDownloadCount(@RequestBody DownloadCountUpdateDto downloadCountUpdateDto) {
        ThrowUtils.throwIf(downloadCountUpdateDto == null, ErrorCode.PARAMS_ERROR);
        Long id = downloadCountUpdateDto.getId();
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Resource resource = resourceService.getById(id);
        Integer downloadCount = resource.getDownloadCount();
        resource.setDownloadCount(downloadCount + 1);
        boolean result = resourceService.updateById(resource);
        return ResultUtils.success(result);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteResource(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = resourceService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
